﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Runtime.InteropServices;

namespace _180FrameWork.Common
{
    public static class MyFileHelper
    {
        public static string SaveRead(string FileName)
        {
            var stream = new FileStream(FileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
            byte[] bytes = new byte[stream.Length];
            stream.Read(bytes, 0, bytes.Length);
            return Encoding.Default.GetString(bytes);
        }

        /// <summary>
        /// 读取指定文件路径的文件内容为字符串
        /// </summary>
        /// <param name="FileName">文件路径</param>
        /// <returns>文件内容</returns>
        public static string Read(string FileName)
        {
            //var stream = new FileStream(FileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
            //stream.re
            StreamReader sr = new StreamReader(FileName, Encoding.Default);
            string str = sr.ReadToEnd();
            sr.Close();
            return str;
        }

        /// <summary>
        /// 将字符串写入指定文件路径的文件
        /// </summary>
        /// <param name="FileName">文件路径</param>
        /// <param name="Text">字符串</param>
        /// <param name="en">格式</param>
        /// <param name="Append">是否追加</param>
        /// <returns>结果</returns>
        public static bool Write(string FileName, string Text, Encoding en, bool Append)
        {
            StreamWriter sw = new StreamWriter(FileName, Append, en);
            sw.Write(Text);
            //关闭流
            sw.Close();
            return true;
            //File.WriteAllText(FileName, Text, en);
            //return true;
        }
        /// <summary>
        /// 读取指定文件路径的文件内容为字符串（带打开文件对话框）
        /// </summary>
        /// <param name="FileName">文件路径</param>
        /// <param name="Filter">文件类型</param>
        /// <returns></returns>
        public static string ReadWithDialog(string FileName, string Filter)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.FileName = FileName;
            ofd.Filter = Filter;
            if (ofd.ShowDialog() == DialogResult.OK)
            {
                return Read(ofd.FileName);
            }
            else
            {
                return "";
            }
        }
        /// <summary>
        /// 读取指定文件路径的复数文件内容为字符串（带打开文件对话框）
        /// </summary>
        /// <param name="FileName"></param>
        /// <param name="Filter"></param>
        /// <returns></returns>
        public static string MultReadWithDialog(string FileName, string Filter)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.Multiselect = true;
            ofd.FileName = FileName;
            ofd.Filter = Filter;
            if (ofd.ShowDialog() == DialogResult.OK)
            {
                StringBuilder strValue = new StringBuilder();
                foreach (string strFileName in ofd.FileNames)
                {
                    strValue.Append(Read(strFileName));
                }
                return strValue.ToString();
            }
            else
            {
                return "";
            }
        }
        /// <summary>
        /// 将字符串写入指定文件路径的文件（带保存文件对话框）
        /// </summary>
        /// <param name="FileName">文件路径</param>
        /// <param name="Text">字符串</param>
        /// <param name="Filter">文件类型</param>
        /// <param name="Append">是否追加</param>
        /// <returns></returns>
        public static string WriteWithDialog(string FileName, string Text, string Filter, Encoding en, bool Append)
        {
            SaveFileDialog sfd = new SaveFileDialog();
            sfd.FileName = FileName;
            sfd.Filter = Filter;
            if (sfd.ShowDialog() == DialogResult.OK)
            {
                if (Write(sfd.FileName, Text, en, Append))
                {
                    return ("文件导出成功，位置在【" + sfd.FileName + "】");
                }
                else
                {
                    return "失败";
                }
            }
            else
            {
                return "取消";
            }
        }

        /// <summary>
        /// 获取文件绝对路径
        /// </summary>
        /// <param name="FileName">文件路径</param>
        /// <param name="Filter">文件类型</param>
        /// <returns></returns>
        public static string FileSelect(string FileName, string Filter)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.FileName = FileName;
            ofd.Filter = Filter;
            if (ofd.ShowDialog() == DialogResult.OK)
            {
                return ofd.FileName;
            }
            else
            {
                return "";
            }
        }
        /// <summary>
        /// 获取文件绝对路径
        /// </summary>
        /// <param name="FileName">文件路径</param>
        /// <param name="Filter">文件类型</param>
        /// <returns></returns>
        public static string[] MultFileSelect(string FileName, string Filter)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.Multiselect = true;
            ofd.FileName = FileName;
            ofd.Filter = Filter;
            if (ofd.ShowDialog() == DialogResult.OK)
            {
                return ofd.FileNames;
            }
            else
            {
                return null;
            }
        }
        /// <summary>
        /// 删除文件
        /// </summary>
        /// <param name="FileName"></param>
        /// <returns></returns>
        public static bool Delete(string FileName)
        {
            File.Delete(FileName);
            return true;
        }

        [DllImport("kernel32.dll", SetLastError = true)]
        static extern bool Wow64DisableWow64FsRedirection(ref IntPtr ptr);
        [DllImport("kernel32.dll", SetLastError = true)]
        static extern bool Wow64RevertWow64FsRedirection(IntPtr ptr);
        /// <summary>
        /// 移动文件
        /// </summary>
        /// <param name="fileToMove"></param>
        /// <param name="fileNewDestination"></param>
        public static void MoveFile(string fileToMove, string fileNewDestination)
        {
            FileInfo file = new FileInfo(fileToMove);
            if (file.Exists && !File.Exists(fileNewDestination))
            {
                try
                {
                    if (Environment.Is64BitOperatingSystem)
                    {
                        IntPtr Wow64value = IntPtr.Zero;
                        Wow64DisableWow64FsRedirection(ref Wow64value);
                        File.Copy(fileToMove, fileNewDestination, true);
                        Wow64RevertWow64FsRedirection(Wow64value);
                    }
                    else
                    {
                        file.CopyTo(fileNewDestination);
                    }

                }
                catch (Exception)
                {
                    throw;
                }
            }
        }
        /// <summary>
        /// 删除文件夹
        /// </summary>
        /// <param name="dir"></param>
        public static void DeleteFolder(string dir)
        {
            foreach (string d in Directory.GetFileSystemEntries(dir))
            {
                if (File.Exists(d))
                {
                    FileInfo fi = new FileInfo(d);
                    if (fi.Attributes.ToString().IndexOf("ReadOnly") != -1)
                        fi.Attributes = FileAttributes.Normal;
                    File.Delete(d);//直接删除其中的文件  
                }
                else
                {
                    DirectoryInfo d1 = new DirectoryInfo(d);
                    if (d1.GetFiles().Length != 0)
                    {
                        DeleteFolder(d1.FullName);////递归删除子文件夹
                    }
                    Directory.Delete(d);
                }
            }
        }
    }
}
